cmake_minimum_required(VERSION 3.16.5)

project(arch C ASM)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)

set(CMAKE_INSTALL_PREFIX ${TOP_DIR}/install)
set(CMAKE_INSTALL_INC_PREFIX ${TOP_DIR}/install/include)

set(SAFETY_FLAGS "-Wall -Wextra -fno-strict-aliasing -static")
#set(SAFETY_FLAGS "-Werror -Wall -Wextra -fno-strict-aliasing")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SAFETY_FLAGS} -D${RUN_TYPE}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SAFETY_FLAGS}")

include(${TOP_DIR}/scripts/ParseConfiguration.cmake)
ParseConfiguration("${TOP_DIR}/milkv_duo_sd_defconfig")

if (CHIP STREQUAL "cv1835")
    add_subdirectory(comm)
elseif (CHIP STREQUAL "cv181x" OR CHIP STREQUAL "cv180x")
    add_subdirectory(comm)
endif()

if(CONFIG_BOARD STREQUAL "cv181x_fpga" OR CONFIG_BOARD STREQUAL "cv181x_fpga_c906")
	add_compile_definitions(FPGA_PORTING)
endif()

add_subdirectory(main)

add_custom_target(cvirtos.bin
    DEPENDS ${CMAKE_INSTALL_PREFIX}/bin/cvirtos.elf
)
add_custom_command(TARGET cvirtos.bin
    COMMAND echo "Build cvirtos.bin and it depends on ${CMAKE_INSTALL_PREFIX}/bin/cvirtos.elf"
    POST_BUILD
    DEPENDS ${CMAKE_INSTALL_PREFIX}/bin/cvirtos.elf
    COMMAND ${CMAKE_OBJCOPY} ARGS ${CMAKE_INSTALL_PREFIX}/bin/cvirtos.elf -O binary ${CMAKE_INSTALL_PREFIX}/bin/cvirtos.bin
    COMMAND ${CMAKE_OBJDUMP} ARGS -dS ${CMAKE_INSTALL_PREFIX}/bin/cvirtos.elf > ${CMAKE_INSTALL_PREFIX}/bin/cvirtos.dis
    VERBATIM
)
